package dao

import (
	"server_cluster/http/model"
)

func (d *Dao) QueryTowerRankByUid(uid uint32) (*model.DbTowerRank, error) {
	towerRank := new(model.DbTowerRank)
	err := d.db.Where("uid = ?", uid).First(towerRank).Error
	if err != nil {
		return nil, err
	}
	return towerRank, nil
}

func (d *Dao) QueryTowerRankList(limit int) ([]*model.DbTowerRank, error) {
	var towerRankList []*model.DbTowerRank = nil
	err := d.db.Find(&towerRankList).Limit(limit).Error
	if err != nil {
		return nil, err
	}
	return towerRankList, nil
}

func (d *Dao) InsertTowerRankList(towerRankList []*model.DbTowerRank) error {
	err := d.db.CreateInBatches(towerRankList, len(towerRankList)).Error
	if err != nil {
		return err
	}
	return nil
}

func (d *Dao) ClearTowerRankTable() error {
	if d.db.Migrator().HasTable(new(model.DbTowerRank)) {
		err := d.db.Migrator().DropTable(new(model.DbTowerRank))
		if err != nil {
			return err
		}
	}
	err := d.db.Migrator().CreateTable(new(model.DbTowerRank))
	if err != nil {
		return err
	}
	return nil
}
